package org.mybatis.generator.codegen.mybatis3.javamapper.elements.suffix;

import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.codegen.mybatis3.SuffixUtil;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Set;
import java.util.TreeSet;

/*********************************************************
 * 文件名称：SuffixDeleteByPrimaryKeyMethodGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.javamapper.elements.suffix
 * 功能说明：带后缀的根据主键删除方法
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/28 16:13
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixDeleteByPrimaryKeyMethodGenerator extends AbstractJavaMapperMethodGenerator {

    private final boolean isSimple;

    public SuffixDeleteByPrimaryKeyMethodGenerator(boolean isSimple) {
        super();
        this.isSimple = isSimple;
    }

    @Override
    public void addInterfaceElements(Interface interfaze) {
        Method method =
                new Method(SuffixUtil.getSuffixMethodName(introspectedTable.getDeleteByPrimaryKeyStatementId(),introspectedTable));
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setAbstract(true);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
        addPrimaryKeyMethodParameters(isSimple, method, importedTypes);

        SuffixUtil.addSuffixParam(importedTypes,method);

        addMapperAnnotations(method);

        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

        if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable)
                && SuffixUtil.isEnableSuffix(introspectedTable)) {
            addExtraImports(interfaze);
            interfaze.addImportedTypes(importedTypes);
            // 删除原方法
            SuffixUtil.deleteOldMethod(interfaze, method);
            interfaze.addMethod(method);
        }
    }

    public void addMapperAnnotations(Method method) {
        // extension point for subclasses
    }

    public void addExtraImports(Interface interfaze) {
        // extension point for subclasses
    }
}
